﻿using Microsoft.EntityFrameworkCore;
using PlatformUW.Contracts;
using PlatformUW.Entities;
using PlatformUW.Entities.RequestFeatures;
using PlatformUW.EntityFramework.Repositories.Extensions;

namespace PlatformUW.EntityFramework.Repositories
{
    public class PlayerRepository : BaseRepository<Player>, IPlayerRepository
    {
        public PlayerRepository(PlatformDBContext repositoryContext) : base(repositoryContext)
        {
        }

        public async Task<PagedList<Player>> GetAllPlayersAsync(PlayerParameter parameter)
        {
            return await Task.FromResult(respositoryContext.Set<Player>()
                .Where(player => player.DateCreated >= parameter.MinDateCreated && player.DateCreated <= parameter.MaxDateCreated)
                .FilterByName(parameter.Account)
                .OrderByQuery(parameter.QueryString)
                .ToPagedList(parameter.PageNumber, parameter.PageSize));
        }

        public Task<Player> GetPlayerByIDAsync(Guid id)
        {
            return Task.FromResult(FindAsync(e => e.Id == id).Result.FirstOrDefault());
        }

        public Task<Player> GetPlayerWithCharByIdAsync(Guid id)
        {
            return Task.FromResult(respositoryContext.Set<Player>()
                .Where(a => a.Id == id)
                .Include(a => a.characters).FirstOrDefault());
        }
    }
}
